package MawsonClient.GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: Ryan Green
 * Date: 12/1/11
 * Time: 8:45 PM
 * Copyright - University of South Australia
 */
public class ButtonPanel extends JPanel {

	int count;
	int pages;
	private JButton nextButton;
	private JLabel progress;
	private JButton previousButton;
	MawsonFrame frame;

	ButtonPanel(MawsonFrame frame) {
		super();

		this.frame = frame;

		//the button panel
		count = 1;

		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), ""));

		progress = new JLabel();
		progress.setHorizontalAlignment(JLabel.CENTER);
		this.add(progress, BorderLayout.CENTER);

		previousButton = new JButton("Previous");
		previousButton.setEnabled(false);
		previousButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				previousPressed();
			}
		});
		this.add(previousButton, BorderLayout.WEST);

		nextButton = new JButton("Next");
		nextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				nextPressed();
			}
		});
		this.add(nextButton, BorderLayout.EAST);
	}

	private void skipValueChanged(boolean selected) {
		if (selected) {
			//disable all components in agg and per line sections and add to skip list
			Component c[] = frame.perAggPanel.getComponents();
			for (int i = 0; i < c.length; i++){
				if (c[i].isVisible()){
					c[i].setEnabled(false);
				}
			}
		} else {
			//enable all components in agg and per line sections and remove from skip list
			Component c[] = frame.perAggPanel.getComponents();
			for (int i = 0; i < c.length; i++){
				if (c[i].isVisible()){
					c[i].setEnabled(true);
				}
			}
		}
	}

	private void nextPressed() {
		//todo ensure all required requested elements are filled

		if (count >= pages) {
			if (frame.config.username == null || frame.config.password == null || frame.config.username.equals("") || frame.config.password.equals("")) {
				if (frame.askUserPass())
					frame.finished();
			} else {
				if (JOptionPane.showConfirmDialog(null, "Are you sure you want send this data to MawsonClient.Mawson?", "Confirm", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
					frame.finished();
			}
		} else {
			count++;
			if (frame.perLineElements.size() > 0) { //if we have per line requests
				CardLayout cl = (CardLayout) (frame.perLinePanel.getLayout());
				cl.next(frame.perLinePanel);
				if (frame.perAggElements.size() > 0) { //if we have both perline and per agg elements
					//check the current count
					CardLayout cl2 = (CardLayout) (frame.perAggPanel.getLayout());
					cl2.show(frame.perAggPanel, frame.aggMap.get(count - 1));
					//display the appropriate card
				}
			} else { //we only have agg requests
				CardLayout cl2 = (CardLayout) (frame.perAggPanel.getLayout());
				cl2.next(frame.perAggPanel);
			}
			update();
		}
	}

	private void previousPressed() {
		count--;
		if (frame.perLineElements.size() > 0) { //if we have per line requests
			CardLayout cl = (CardLayout) (frame.perLinePanel.getLayout());
			cl.previous(frame.perLinePanel);
			if (frame.perAggElements.size() > 0) { //if we have both perline and per agg elements
				//check the current count
				CardLayout cl2 = (CardLayout) (frame.perAggPanel.getLayout());
				cl2.show(frame.perAggPanel, frame.aggMap.get(count - 1));
				//display the appropriate card
			}
		} else { //we only have agg requests
			CardLayout cl2 = (CardLayout) (frame.perAggPanel.getLayout());
			cl2.previous(frame.perAggPanel);
		}
		update();
	}

	public void update() {
		if (pages == 1) {
			//only one page
			previousButton.setVisible(false);
			nextButton.setText("Send");
			progress.setText("1/1");
		} else if (count == pages) {
			//we're at the last page
			previousButton.setEnabled(true);
			nextButton.setText("Send");
			progress.setText(Integer.toString(count) + "/" + Integer.toString(pages));
		} else if (count == 1) {
			//we're at the first page of multiple pages
			previousButton.setEnabled(false);
			nextButton.setText("Next");
			progress.setText("1/" + Integer.toString(pages));
		} else {
			//we're somewhere in-between
			previousButton.setEnabled(true);
			nextButton.setText("Next");
			progress.setText(Integer.toString(count) + "/" + Integer.toString(pages));
		}

	}
}
